package com.kch.system.service.impl;

import com.kch.system.domain.Meeting;
import com.kch.system.mapper.MeetingMapper;
import com.kch.system.service.IMeetingService;
import com.kch.system.vo.MeetingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MeetingServiceImpl implements IMeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<MeetingVO> selectMeetingPage(Meeting meeting) {
        return meetingMapper.seletMeetingPage(meeting);
    }

    @Override
    public int updateStatus(Integer id) {
        Meeting meeting = meetingMapper.selectByPrimaryKey(id);
        String status = ("0".equals(meeting.getStatus())? "1" : "0");
        return meetingMapper.updateStatus(meeting.getId(), status);
    }

    @Override
    public int insertSysMeeting(Meeting meeting) {
        meeting.setStatus("1");
        meeting.setCreateTime(new Date());
        return meetingMapper.insert(meeting);
    }

    @Override
    public int deleteMeeting(String ids) {
        return meetingMapper.deleteByPrimaryKey(Integer.valueOf(ids));
    }

    @Override
    public MeetingVO selectById(Integer id) {
        Meeting meeting = meetingMapper.selectByPrimaryKey(id);
        MeetingVO meetingVO = new MeetingVO();
        BeanUtils.copyProperties(meeting, meetingVO);
        meetingVO.setHoldTimeTxt(format.format(meeting.getHoldTime()));
        meetingVO.setRegTimeTxt(format.format(meeting.getRegTime()));
        return meetingVO;
    }

    @Override
    public int updateById(MeetingVO meetingVO) throws Exception {
        Meeting meeting = meetingMapper.selectByPrimaryKey(meetingVO.getId());
        meetingVO.setCreateBy(meeting.getCreateBy());
        meetingVO.setCreateTime(meeting.getCreateTime());
        meetingVO.setStatus(meeting.getStatus());
        BeanUtils.copyProperties(meetingVO, meeting);
        meeting.setHoldTime(format.parse(meetingVO.getHoldTimeTxt()));
        meeting.setRegTime(format.parse(meetingVO.getRegTimeTxt()));
        meeting.setUpdateTime(new Date());
        return meetingMapper.updateByPrimaryKeyWithBLOBs(meeting);
    }
}
